package com.itheima.myreflect.Demo2;

import com.itheima.myreflect.Demo1.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class demo2 {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
        // 通过无参构造创建Student的对象
        Class<Student> clazz = Student.class;
        Student student = clazz.newInstance();
        System.out.println("student = " + student);

        //
        Constructor<Student> constructor1 = clazz.getConstructor(String.class, int.class);
        Student student1 = constructor1.newInstance("张三", 23);
        System.out.println("student1 = " + student1);

        Class clazzMath = Class.forName("java.lang.Math");
        Constructor constructor2 = clazzMath.getDeclaredConstructor();

        constructor2.setAccessible(true);
        Math math =(Math) constructor2.newInstance();
        double random = math.random();
        System.out.println("random = " + random);
    }
}
